import Vue from "vue";

/**
 * Created by shidaiapp on 2017/9/29.
 */

function changeModelLoading(self, tiyData) {
  tiyData.tStatus.modalClear.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalClear.isLoading = true;
  });

}
/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  //resetForm(self, tiyData);
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 20;
  tiyData.tPage.currentPage = 1;
  tiyData.tParams.userName = '';
  tiyData.tParams.functionName = '';
  tiyData.tParams.startTime = "";
  tiyData.tParams.endTime = "";
};

/**
 * 清理操作记录
 * @param self
 * @param tiyData
 */
export const webClearOperationRecord = function (self, tiyData) {
  tiyData.tStatus.modalClear.isLoading = true;
  self.$apiRole.clearOperationRecord()
    .then(function (response) {
      tiyData.tStatus.modalClear.isShow = false;
      changeModelLoading(self, tiyData);
      if (response.success) {
        self.$Message.success('清除成功!');
      } else {
        tiyData.tStatus.modalClear.isLoading = false;
        self.$Message.error(response.errorMessage);
      }
    });
};



/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webList = (self, tiyData) => {
  self.$Spin.show();
  let data = {"pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "userName": tiyData.tParams.userName,
    "orderBy": tiyData.tParams.orderBy,
    "functionName": tiyData.tParams.functionName
  };
  //iview时间控件显示为utc时间，会转成本地时间，这里再转回来
  if(tiyData.tParams.startTime){
    data.startDate = Vue.prototype.$tiyQueryTime(new Date(tiyData.tParams.startTime).getTime());
  }
  if(tiyData.tParams.endTime){
    data.endDate = Vue.prototype.$tiyQueryTime(new Date(tiyData.tParams.endTime).getTime());

  }
  self.$apiRole.getUserOperationList(data).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.historyList.splice(0);
      let historyPage = response.result;
      tiyData.tPage.total = historyPage.total;
      tiyData.tPage.stepNumber = historyPage.pageSize;
      historyPage.list.forEach(
        function (item) {
          tiyData.tData.historyList.push(item);
        }
      );
      if(0 == tiyData.tData.historyList.length){
        self.$Message.error("查询没有结果");
      }
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};


